package stack

type ListElement struct {
	value interface{}
	next  *ListElement
}

type ListStack struct {
	size int
	len  int
	root *ListElement
}

func NewListStack(size int) *ListStack {
	return &ListStack{
		size: size,
		len:  0,
		root: &ListElement{},
	}
}

func (l *ListStack) Full() bool {
	return l.size == l.len
}

func (l *ListStack) Push(value interface{}) bool {
	if l.Full() {
		return false
	}
	next := l.root.next
	e := &ListElement{
		value: value,
		next:  next,
	}
	l.root.next = e
	l.len++
	return true
}

func (l *ListStack) Pop() interface{} {
	next := l.root.next
	if next == nil {
		return nil
	}
	l.root.next = next.next
	next.next = nil
	l.len--
	return next.value
}

func (l *ListStack) Peek() interface{} {
	next := l.root.next
	if next != nil {
		return next.value
	}
	return nil
}
